<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2020/1/4
// +----------------------------------------------------------------------

namespace app\common\services;
use AlibabaCloud\Client\AlibabaCloud;
use app\common\facade\Arr;

/**
 * 阿里云短信服务类
 * Class AliyunSmsServicer
 * @package app\common\services
 * @author feraner<feraner@163.com>
 * @date 2020/1/4
 */
class AliyunSmsServicer
{


    /**
     * 所属区域 API支持的RegionID
     * @var string
     */
    private $regionId = 'cn-hangzhou';

    /**
     * 短信服务 host
     * @var string
     */
    private $smsHost = 'dysmsapi.aliyuncs.com';


    /**
     * 短信验证码 签名名称
     * @var string
     */
    private $codeSignName = '掌上汇同城';//温州尚跃科技有限公司

    /**
     * 短信验证码 模版ID
     * @var string
     */
    private $codeTemplateCode = 'SMS_181867468';


    /**
     * AliyunSmsServicer constructor.
     * @throws \AlibabaCloud\Client\Exception\ClientException
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function __construct()
    {
        //阿里云api 默认请求client
        AlibabaCloud::accessKeyClient(
            sysconf('aliyun_accessKeyId'),
            sysconf('aliyun_accessSecret')
        )->regionId($this->regionId)->asDefaultClient();


    }


    /**
     * 发送短信验证码
     * @param $phone [电话号码]
     * @param $code [短信验证码]
     *
     * @return bool
     * @throws \Exception
     */
    public function sendSmsCode($phone, $code)
    {
        try {
            $response = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host($this->smsHost)
                ->options([
                    'query' => [
                        'RegionId'          => $this->regionId,
                        'PhoneNumbers'      => $phone,
                        'SignName'          => $this->codeSignName,
                        'TemplateCode'      => $this->codeTemplateCode,
                        'TemplateParam'     => json_encode(['code' => $code])
                    ],
                ])
                ->request();
            $result = $response->toArray();
            if (Arr::valueEq($result, 'Code', 'OK')) {
                return true;
            } else {
                return false;
            }
        } catch (\Exception $e) {
            throw $e;
        }
        return false;
    }



}
